package com.tuanzi.loan.business.repository.compensation.impl;

import com.tuanzi.loan.business.repository.compensation.CompensationOrderRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.compensation.CompensationOrderRequest;
import com.tuanzi.loan.vo.response.compensation.CompensationOrderResponse;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCardCompensationOrder.LOAN_CARD_COMPENSATION_ORDER;

/**
 * @author administrator
 * @createTime 2019-06-04 15:44
 */
@Repository
public class CompensationOrderRepositoryImpl extends JOOQBaseRepositoryPlus implements CompensationOrderRepository{
    @Override
    public Page<CompensationOrderResponse> list(CompensationOrderRequest searchRequest, Pageable pageable) {
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        List<String> productIds = searchRequest.getProductIds();
        List<Integer> channelIds = searchRequest.getChannelIds();
        Integer userType = searchRequest.getUserType();
        Long orderId = searchRequest.getOrderId();
        String phone = searchRequest.getPhone();
        String minAmount = searchRequest.getMinAmount();
        String maxAmount = searchRequest.getMaxAmount();
        Integer orderType = searchRequest.getOrderType();
        Integer status = searchRequest.getStatus();
        String from = searchRequest.getFrom();
        Integer isUseCoupon = searchRequest.getIsUseCoupon();
        fields(LOAN_CARD_COMPENSATION_ORDER.ID,
                LOAN_CARD_COMPENSATION_ORDER.CTIME,
                COMM_PRODUCT_CHANNEL.CHANNEL_NAME,
                COMM_PRODUCT_PRDID.NAME,
                LOAN_CARD_COMPENSATION_ORDER.USER_TYPE,
                LOAN_CARD_COMPENSATION_ORDER.PETNAME,
                LOAN_CARD_COMPENSATION_ORDER.MOBNUM,
                LOAN_CARD_COMPENSATION_ORDER.ORDER_TYPE,
                LOAN_CARD_COMPENSATION_ORDER.AMOUNT,
                LOAN_CARD_COMPENSATION_ORDER.STATUS,
                LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM,
                LOAN_CARD_COMPENSATION_ORDER.REMARK,
                LOAN_CARD_COMPENSATION_ORDER.FEE,
                LOAN_CARD_COMPENSATION_ORDER.FEE_TYPE,
                LOAN_CARD_COMPENSATION_ORDER.IS_USE_COUPON
        );
        leftJoin(COMM_PRODUCT_CHANNEL,COMM_PRODUCT_CHANNEL.CHANNEL_CODE.eq(LOAN_CARD_COMPENSATION_ORDER.CHANNEL));
        leftJoin(COMM_PRODUCT_PRDID,LOAN_CARD_COMPENSATION_ORDER.PRODUCT_ID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)));
        eq(LOAN_CARD_COMPENSATION_ORDER.TYPE,20);
        between(LOAN_CARD_COMPENSATION_ORDER.CTIME,startTime,endTime);
        in(LOAN_CARD_COMPENSATION_ORDER.PRODUCT_ID,productIds);
        in(LOAN_CARD_COMPENSATION_ORDER.CHANNEL,channelIds);
        eq(LOAN_CARD_COMPENSATION_ORDER.USER_TYPE,userType);
        eq(LOAN_CARD_COMPENSATION_ORDER.ID,orderId);
        eq(LOAN_CARD_COMPENSATION_ORDER.MOBNUM,phone);
        between(LOAN_CARD_COMPENSATION_ORDER.AMOUNT,minAmount,maxAmount);
        eq(LOAN_CARD_COMPENSATION_ORDER.ORDER_TYPE,orderType);
        gt(LOAN_CARD_COMPENSATION_ORDER.STATUS,1);
        eq(LOAN_CARD_COMPENSATION_ORDER.STATUS,status);
        eq(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM,from);
        eq(LOAN_CARD_COMPENSATION_ORDER.IS_USE_COUPON,isUseCoupon);
        orderBy(LOAN_CARD_COMPENSATION_ORDER.ID, Sort.Direction.DESC);
        return search(pageable, e -> {
            CompensationOrderResponse response = new CompensationOrderResponse();
            response.setCreateTime(e.get(LOAN_CARD_COMPENSATION_ORDER.CTIME));
            response.setId(e.get(LOAN_CARD_COMPENSATION_ORDER.ID));
            response.setProductName(e.get(COMM_PRODUCT_PRDID.NAME));
            response.setChannelName(e.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            response.setUserType(e.get(LOAN_CARD_COMPENSATION_ORDER.USER_TYPE));
            response.setUsername(PhoneNumNideUtil.hideName(e.get(LOAN_CARD_COMPENSATION_ORDER.PETNAME)));
            response.setPhone(e.get(LOAN_CARD_COMPENSATION_ORDER.MOBNUM));
            response.setOrderType(e.get(LOAN_CARD_COMPENSATION_ORDER.ORDER_TYPE));
            response.setAmount(e.get(LOAN_CARD_COMPENSATION_ORDER.AMOUNT));
            response.setStatus(e.get(LOAN_CARD_COMPENSATION_ORDER.STATUS));
            response.setFrom(e.get(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM));
            response.setFee(e.get(LOAN_CARD_COMPENSATION_ORDER.FEE));
            response.setRemark(e.get(LOAN_CARD_COMPENSATION_ORDER.REMARK));
            response.setIsUseCoupon(e.get(LOAN_CARD_COMPENSATION_ORDER.IS_USE_COUPON));
            return response;
        });
    }

    @Override
    public List<CompensationOrderResponse> totalList(CompensationOrderRequest searchRequest) {

        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        List<String> productIds = searchRequest.getProductIds();
        List<Integer> channelIds = searchRequest.getChannelIds();
        Integer userType = searchRequest.getUserType();
        Long orderId = searchRequest.getOrderId();
        String phone = searchRequest.getPhone();
        String minAmount = searchRequest.getMinAmount();
        String maxAmount = searchRequest.getMaxAmount();
        Integer orderType = searchRequest.getOrderType();
        Integer status = searchRequest.getStatus();
        String from = searchRequest.getFrom();
        Integer isUseCoupon = searchRequest.getIsUseCoupon();
        fields(LOAN_CARD_COMPENSATION_ORDER.ID,
                LOAN_CARD_COMPENSATION_ORDER.AMOUNT,
                LOAN_CARD_COMPENSATION_ORDER.STATUS,
                LOAN_CARD_COMPENSATION_ORDER.FEE
        );
        eq(LOAN_CARD_COMPENSATION_ORDER.TYPE,20);
        between(LOAN_CARD_COMPENSATION_ORDER.CTIME,startTime,endTime);
        in(LOAN_CARD_COMPENSATION_ORDER.PRODUCT_ID,productIds);
        in(LOAN_CARD_COMPENSATION_ORDER.CHANNEL,channelIds);
        eq(LOAN_CARD_COMPENSATION_ORDER.USER_TYPE,userType);
        eq(LOAN_CARD_COMPENSATION_ORDER.ID,orderId);
        eq(LOAN_CARD_COMPENSATION_ORDER.MOBNUM,phone);
        between(LOAN_CARD_COMPENSATION_ORDER.AMOUNT,minAmount,maxAmount);
        eq(LOAN_CARD_COMPENSATION_ORDER.ORDER_TYPE,orderType);
        gt(LOAN_CARD_COMPENSATION_ORDER.STATUS,1);
        eq(LOAN_CARD_COMPENSATION_ORDER.STATUS,status);
        eq(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM,from);
        eq(LOAN_CARD_COMPENSATION_ORDER.IS_USE_COUPON,isUseCoupon);
        orderBy(LOAN_CARD_COMPENSATION_ORDER.ID, Sort.Direction.DESC);
        return search(CompensationOrderResponse.class);
       /* return search(pageable, e -> {
            CompensationOrderResponse response = new CompensationOrderResponse();
            response.setCreateTime(e.get(LOAN_CARD_COMPENSATION_ORDER.CTIME));
            response.setId(e.get(LOAN_CARD_COMPENSATION_ORDER.ID));
            response.setProductName(e.get(COMM_PRODUCT_PRDID.NAME));
            response.setChannelName(e.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            response.setUserType(e.get(LOAN_CARD_COMPENSATION_ORDER.USER_TYPE));
            response.setUsername(e.get(LOAN_CARD_COMPENSATION_ORDER.PETNAME));
            response.setPhone(e.get(LOAN_CARD_COMPENSATION_ORDER.MOBNUM));
            response.setOrderType(e.get(LOAN_CARD_COMPENSATION_ORDER.ORDER_TYPE));
            response.setAmount(e.get(LOAN_CARD_COMPENSATION_ORDER.AMOUNT));
            response.setStatus(e.get(LOAN_CARD_COMPENSATION_ORDER.STATUS));
            response.setFrom(e.get(LOAN_CARD_COMPENSATION_ORDER.CHANNEL_FROM));
            response.setFee(e.get(LOAN_CARD_COMPENSATION_ORDER.FEE));
            response.setRemark(e.get(LOAN_CARD_COMPENSATION_ORDER.REMARK));
            return response;
        });*/
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_CARD_COMPENSATION_ORDER;
    }
}
